package com.combetter.changxing.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clj.fastble.BleManager;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.combetter.changxing.R;
import com.combetter.changxing.bluetooth.FacilityClient2;
import com.combetter.changxing.collections.PerfectClickListener;
import com.combetter.changxing.db.entity.Facility;
import com.combetter.changxing.util.LogUtil;

import java.util.List;

import static com.combetter.changxing.bluetooth.FacilityClient2.AUTH_SUCCESS;


/**
 * 类名  已配对设备Adapter
 * 作者  tlfx: Sean.guo
 * 日期  2019/3/16 16:45
 */
public class FacilityAdapter extends BaseQuickAdapter<Facility, BaseViewHolder> {
    private Context mContext;
    private FacilityInterface mClick;

    public void setFacilityClickListener(FacilityInterface click) {
        this.mClick = click;
    }

    public FacilityAdapter(Context context, @Nullable List<Facility> data) {
        super(R.layout.fragment_facility_item, data);
        this.mContext = context;
    }

    public void clearAllData() {
        if (this.getData() != null && getData().size() > 0) {
            this.getData().clear();
        }
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, Facility item) {
        SwipeMenuLayout swipeLayout = helper.getView(R.id.swipe_layout);
        helper.setText(R.id.name, item.getFacilityName() + "  测试显示： " + item.getFacilityAddress());
        boolean isConnected = (BleManager.getInstance().isConnected(item.getFacilityAddress()));
        if (isConnected && FacilityClient2.mAuthStatu == AUTH_SUCCESS) {
            helper.setText(R.id.tv_con, isConnected ? "已连接" : "未连接");
            helper.setTextColor(R.id.tv_con, isConnected ? mContext.getResources().getColor(R.color.red)
                    : mContext.getResources().getColor(R.color.title_green));
        } else {
            helper.setText(R.id.tv_con, "未连接");
            helper.setTextColor(R.id.tv_con, mContext.getResources().getColor(R.color.title_green));
        }
        helper.getView(R.id.tv_con).setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (mClick != null) {
                    LogUtil.e("---name:" + item.getFacilityName() + "  mac: " + item.getFacilityAddress());
                    if (BleManager.getInstance().isConnected(item.getFacilityAddress())) {
                        mClick.viewDisconnect(item);
                    } else if (!BleManager.getInstance().isConnected(item.getFacilityAddress())) {//断开其他的，链接最新的
                        mClick.viewConnect(item);
                    } else {
                        LogUtil.e("-------特殊情况。。。。。");
                    }
                }
            }
        });
        /**
         * 重命名onStartConnect
         */
        helper.getView(R.id.tv_rechristen).setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                swipeLayout.quickClose();
                if (mClick != null) {
                    mClick.viewRename(item);
                }
            }
        });
        /**
         * 取消配对
         */
        helper.getView(R.id.tv_cancel).setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                swipeLayout.quickClose();
                if (mClick != null) {
                    if (BleManager.getInstance().isConnected(item.getFacilityAddress())) {
                        mClick.viewCancelMate(item, true);
                    } else {
                        mClick.viewCancelMate(item, false);
                    }
                }
            }
        });
    }

    public interface FacilityInterface {
        void viewConnect(Facility item);

        void viewDisconnect(Facility item);

        void viewRename(Facility item);

        void viewCancelMate(Facility item, boolean isConnect);
    }
}
